﻿using System;
using Froggy.Stocks.Analysis;

namespace Froggy.Stocks.Analysis.Candles
{
    public class DojiAnalysis : IAnalysis
    {
        #region IAnalysisUnit Members

        public int Period
        {
            get;
            set;
        }

        public IPattern Analyse(IQuotationSnapshot qs)
        {

            var open = qs.OpenValue;
            var high = qs.MaxValue;
            var low = qs.MinValue;
            var close = qs.CloseValue;
            var sizeOfUpperShadow = qs.Candle.SizeOfUpperShadow;
            var sizeOfLowerShadow = qs.Candle.SizeOfLowerShadow;
            var relationBetweenUpperAndLowerShadow = sizeOfUpperShadow / sizeOfLowerShadow;

            var openAndCloseNear = Math.Abs(open - close) < 0.0002;
            var hasUpperShadow = sizeOfUpperShadow > 0.0001;
            var hasLowerShadow = sizeOfLowerShadow > 0.0001;
            var isDoji = openAndCloseNear && hasUpperShadow && hasLowerShadow;
            if (!isDoji)
            {
                return null;
            }

            var pattern = new DojiPattern
                              {
                                  Strength = -1,
                                  QuotationShapshot = qs
                              };
            if ((sizeOfUpperShadow > 0.0004) && (sizeOfLowerShadow > 0.0004) &&
                (relationBetweenUpperAndLowerShadow > 0.8))
            {
                pattern.Type = "Long Legged";
            }
            else if (relationBetweenUpperAndLowerShadow < 0.1)
            {
                pattern.Type = "Dragonfly";
            }
            else if (relationBetweenUpperAndLowerShadow > 0.1)
            {
                pattern.Type = "Gravestone";
            }
            return pattern;
        }

        #endregion
    }
}